/**
 * 格式化时间
 */
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
/**
 * 格式化千分位金额并返回
 */
const formatMoney = value => {
  if (Number(value) === 0) {
    return 0.00
  }
  var foramtMoney = value.toLocaleString('en-US')
  return foramtMoney
}

/**
 * 金额String转Int
 */
const intFormatMoney = value => {
  let moneyIndex = value.indexOf(',');
  if (moneyIndex != -1) {
    let moneyView = value.replace(',', '')
    return Number(moneyView)
  } else {
    return Number(value)
  } 
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

module.exports = {
  formatTime: formatTime,
  formatMoney: formatMoney,
  intFormatMoney: intFormatMoney
}
